async function rendercouse(data){
    
    await rendertable(data=data,domhtml='.course tbody',domdata=['id','coursename','major','classname','timestamp','messages'])
    dataform['classinfodata']=JSON.parse(JSON.stringify(data))
    console.log(dataform);
    for (const i in dataform['classinfodata']){
        $(`.course tbody tr:eq(${i})`).append(`<td><button class="layui-btn layui-bg-red" id="${data[i].id}" onclick="delcourse(this)">删除</button></td>`)
     }
}
async function delcourse(even){
 let valueid=parseInt(even.id)
 response=await taskObject.fetchtemplate(path='/api/admin/course?valueid='+valueid,method='DELETE',data={})
 if(response.code!=200){
   layer.msg(response.message,{icon:2,time:1500})
   return ;
 }
 layer.msg("删除成功",{icon:1,time:1500})
 setTimeout(()=>{
   window.location.reload()
 },2000)
 return ;
}
!(async function () {  
    response=await taskObject.fetchtemplate(path='/api/admin/course')
    await rendercouse(response.data)
    $('.search').on('click',async function (){
        let value=$('.course input[type="text"]').val()
         if (value==''){
            layer.msg('请输入搜索内容',{icon:2,time:1000})
            return ;
         }
         response=await taskObject.fetchtemplate(path='/api/admin/course?name='+value)
         if(response.code==200){
            layer.msg('搜索成功',{icon:1,time:1000})
            setTimeout(async ()=>{
               await rendercouse(data=response.data)
            },1100)
            return ;
         }
       
    })//搜索


    $('.course i').click(async ()=>{
        let collegenamedata=[]
        let collegelisthtml=' <option value="">请选择学院</option>'
        dataform['majordata'].forEach((item)=>{
         if(collegenamedata.length==0){
         collegenamedata.push(item.collegename)
         collegelisthtml+=`<option value="${item.collegename}">${item.collegename}</option>`
         }
         collegenamedata.forEach((key)=>{
            if(key!=item.collegename){
               collegenamedata.push(item.collegename)
               collegelisthtml+=`<option value="${item.collegename}">${item.collegename}</option>`
            }
         })
         
      })
        let teacherhtml='<option value="">请选择教师</option>'
        dataform.teacherdata.forEach((teacher)=>{
            teacherhtml+=`<option value="${teacher.username}">${teacher.username}</option>`
        })
        let a=`<form class="layui-form">
        <div class="layui-form-item">
         <label for="" class="layui-form-label">课程名</label>
         <div class="layui-input-block">
            <input type="text" name="course" class="layui-input" required>
         </div>
        </div>
        <div class="layui-form-item">
         <label for="" class="layui-form-label">学院</label>
         <div class="layui-input-block">
            <select name="collegename" id="" lay-filter="collegename">
               <option value="">请选择学院</option>
            </select>
         </div>
        </div>
        <div class="layui-form-item">
        <label for="" class="layui-form-label">课程描述</label>
        <div class="layui-input-block">
          <input class="layui-input" name="messages" type="text"></input>
        </div>
       </div>
        <div class="layui-form-item">
         <label for="" class="layui-form-label">班级</label>
         <div class="layui-input-block">
            <select name="classname" id="">
               <option value="">请选择班级</option>
            </select>
         </div>
        </div>
        <div class="layui-form-item">
         <label for="" class="layui-form-label">授课教师</label>
         <div class="layui-input-block">
            <select name="teacher" id="">`+teacherhtml+`</select>
         </div>
        </div>
        <div class="layui-form-item">
        <div class="layui-input-block"> <button class="layui-btn layui-bg-blue" lay-submit lay-filter="course">添加</button>
        </div>
         </div>
        </form>`
        layer.open({
         title:"编辑学生",
         area: ['500px', '400px'],
         content: a,
         btn: [],
         success: function (layero, index) {
            let form=layui.form;
            console.log(collegelisthtml);
            $('select[name="collegename"]').html(collegelisthtml)
            form.render()
            form.on('select(collegename)',(collegedata)=>{
               let collegename=collegedata.value;
               let majorlist=[]
               dataform.majordata.forEach((item)=>{
                  if(item.collegename==collegename){
                     majorlist.push(item.major)
                  }
               })
               let classhtml='<option value="">请选择班级</option>'
               majorlist.forEach((key)=>{
                  dataform.classinfodata1.forEach((item)=>{
                     if(item.major==key){
                        classhtml+=`<option value="${item.classname}">${item.classname}</option>`
                     }
                  })
               })
               $('select[name="classname"]').html(classhtml)
               form.render()
            })
            form.on('submit(course)',(data)=>{
               Object.keys(data.field).forEach((key)=>{
                  if(data.field[key]==''){
                     layer.msg("请完善表单",{icon:2,time:1500})
                     return false
                  }
               })
               !(async function(){
                  response=await taskObject.fetchtemplate(path='/api/admin/course',method='POST',data=data.field)
                  if(response['code']!=200){
                     layer.msg(response.message,{icon:2,time:2000})
                     return false
                  }
                  layer.msg('添加课程成功',{icon:1,time:1000})
                  setTimeout(()=>{
                     window.location.reload()
                  },1500)
                  return false;
               }())
               return false
            })
            
         }
         })
    })
      

}())